import javax.swing.*;
import java.awt.*;
import java.util.*;

/**
 *	Homework 10
 * Tile Class
 *
 * This class defines the properties of each tile, and has methods for accessing and altering each tile's data.
 * I worked on the homework assignment alone, using only course materials.
 *
 *  @author Christopher Cheape
 *  @version 1.0 11/30/12
 */
 
public class Tile implements MineTile
{
	
	private boolean hasMine;
	private JButton tileButton;
	private int row, col;
	private GameController controller;
	private boolean hasFlag;
	
	/**
	 * Contructor for the Tile. Each Tile has a row, a column, and a JButton.
	 *
	 * @param yourRow the row of this Tile
	 * @param yourCol the column of this Tile
	 * @param yourCont the GameController for this Tile.
	 */
	 
	public Tile(int yourRow, int yourCol,GameController yourCont)
	{
		controller = yourCont;
		row = yourRow;
		col = yourCol;
		tileButton = new JButton();
		tileButton.setMargin(new Insets(0, 0, 0, 0));
		tileButton.setPreferredSize(new Dimension(20,20));
		hasMine = false;
		hasFlag = false;
	}
	
	/**
	 * Gets the row of this tile.
	 * 
	 * @return int the row.
	 */
	public int getRow()
	{
		return row;
	}
	
	/**
	 * Gets the column of this tile.
	 * 
	 * @return int the col.
	 */
	public int getCol()
	{
		return col;
	}
	
	/**
	 * Gets the number of tiles adjacent to this one with mines.
	 * 
	 * @return int the number of adjacent tiles with mines.
	 */
	public int getAdjacent()
	{
		int num = 0;
		ArrayList<Tile> adjacent = MinesweeperUtils.getAdjacent(this.getRow(), this.getCol(), controller.getTileList());
		
		for(Tile tile : adjacent)
		{
			if(tile.gotMine() == true)
			{
				num++;
			}
		}
		return num;
	}
	
	/**
	 * Gets the tile's JButton
	 * 
	 * @return JButton the tiles JButton
	 */
	public JButton getButton()
	{
		return tileButton;
	}
	
	/**
	 * Sets the tile's JButton to a new JButton.
	 * 
	 * @param newButton the new JButton.
	 */
	public void setButton(JButton newButton)
	{
		tileButton = newButton;
	}
	
	/**
	 * Returns whether or not the Tile has a mine.
	 * 
	 * @return boolean if the tile has a mine.
	 */
	public boolean gotMine()
	{
		return hasMine;
	}
	
	/**
	 * Sets whether or not the Tile has a mine.
	 * 
	 * @param boolean if the tile has a mine.
	 */
	public void setMine(boolean boo)
	{
		hasMine = boo;
	}
	
	/**
	 * Returns whether or not the Tile has a flag.
	 * 
	 * @return boolean if the tile has a flag.
	 */
	public boolean gotFlag()
	{
		return hasFlag;
	}
	
	/**
	 * Sets whether or not the Tile has a flag.
	 * 
	 * @param boolean if the tile has a flag.
	 */
	public void setFlag(boolean boo)
	{
		hasFlag = boo;
	}

}
